package com.sugon.controller.device;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.consts.AnnexFileModuleType;
import com.sugon.dao.DevEquFileDao;
import com.sugon.entity.*;
import com.sugon.entity.dto.DevEquFileDTO;
import com.sugon.entity.dto.Device2KnowbaseParam;
import com.sugon.service.*;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备档案附件关系类Controller
 *
 * @author sugon
 * @date 2020-12-24 10:59:39
 */
@Controller
@RequestMapping("devequfile")
public class DevEquFileController {

    @Autowired
    private DevEquFileService devEquFileService;
    @Autowired
    private DevEquFileDao devEquFileDao;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private FileTypeService fileTypeService;
    @Autowired
    private KnowBaseService knowBaseService;
    @Autowired
    private KnowFileService knowFileService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        String devId = MapUtil.getStr(params, "devId");
        List<DevEquFileDTO> list = devEquFileDao.queryDevEquFile(devId);
        PageUtils pageUtil = new PageUtils(list, list.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 纳入知识库
     */
    @NonBusinessParamValid
    @RequestMapping("/saveKnow")
    @RequiresPermissions("annexfile:save")
    @ResponseBody
    public R saveKnow(@Validated @RequestBody Device2KnowbaseParam device2KnowbaseParam, BindingResult bindingResult) {
        // 校验设备是否存在
        String devId = device2KnowbaseParam.getDevId();
        DeviceInfoEntity device = deviceInfoService.queryObject(devId);
        if (device == null) {
            return R.error("设备不存在，请确认！");
        }
        // 校验资料类别是否存在
        String fileTypeId = device2KnowbaseParam.getFileTypeId();
        FileTypeEntity fileType = fileTypeService.queryObject(fileTypeId);
        if (fileType == null) {
            return R.error("资料类别不存在，请确认！");
        }
        // 校验知识库是否存在
        String knowbaseId = device2KnowbaseParam.getKnowbaseId();
        KnowBaseEntity knowBase = knowBaseService.queryObject(knowbaseId);
        if (knowBase == null) {
            return R.error("知识库不存在，请确认！");
        }

        /*
            找出设备指定类型的所有附件，然后纳入到知识库中

         */
        List<DevEquFileEntity> devEquFileList = devEquFileService.queryList(devId, fileTypeId);
        if (CollectionUtil.isEmpty(devEquFileList)) {
            return R.error("当前资料类型下尚未添加资料，请确认！");
        }
        KnowFileEntity knowFile = new KnowFileEntity();
        knowFile.setKnowId(knowbaseId);
        knowFile.setType(AnnexFileModuleType.DEVICE);
        for (DevEquFileEntity equFile : devEquFileList) {
            knowFile.setFileId(equFile.getFileId());
            knowFileService.save(knowFile);
        }
        return R.ok();
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevEquFileEntity devEquFile = devEquFileService.queryObject(id);

        return R.ok().put("devEquFile", devEquFile);
    }

    /**
     * 保存
     */
    @AutoLog("设备档案附件关系类--保存")
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody DevEquFileEntity devEquFile) {
        devEquFileService.save(devEquFile);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("设备档案附件关系类--修改")
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody DevEquFileEntity devEquFile) {
        devEquFileService.update(devEquFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("设备档案附件关系类--删除")
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devEquFileService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevEquFileEntity> list = devEquFileService.queryList(params);

        return R.ok().put("list", list);
    }
}
